package com.horse.cloud.member.user.controller;

import com.horse.cloud.framework.base.BaseController;
import com.horse.cloud.framework.general.enums.ResponseCodeEnums;
import com.horse.cloud.framework.general.response.Response;
import com.horse.cloud.framework.general.response.ResponseReceive;
import com.horse.cloud.framework.redis.DistributedReentrantLock;
import com.horse.cloud.member.user.feignclients.MemberAccountClient;
import com.horse.cloud.member.user.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @create : Created by intelliJ IDEA 17.2
 * @author : bing.Pan
 * @e-mail : 15923508369@163.com
 * @gmdate : 09 28 2017 15:43
 * @sidesc :
 */
@RestController
public class TestController extends BaseController {


    @Autowired private TestService testService;
    @Autowired private MemberAccountClient memberAccountClient;





    @RequestMapping(value = "/testFeign")
    public Object testFeign(){
        try{

            Response response = ResponseReceive.receive(memberAccountClient.getMemberAccount());
            return new Response<>(response.getData());

        }catch (Exception exception){
            LOGGER.info("【cloud-member-user】 调用[/memberAccount/getMemberAccount]异常");
            return  new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK);
        }
    }




    @RequestMapping(value = "/testRedis")
    public Object testRedis(){



        LOGGER.info("【cloud-member-user】 test logback {}","占位符测试运行流程");



        redisClient.leftPush("testList","values-001");
        redisClient.leftPush("testList","values-002");
        redisClient.rightPuah("testList","values-003");
        redisClient.rightPuah("testList","values-004");



        redisClient.leftPush("testListExpireTime","values-001",60L,TimeUnit.SECONDS);
        redisClient.leftPush("testListExpireTime","values-002");
        redisClient.leftPush("testListExpireTime","values-003");
        redisClient.leftPush("testListExpireTime","values-004");

        redisClient.set("测试redis集群"+ System.currentTimeMillis(),"测试redis集群"+ System.currentTimeMillis());

        redisClient.setNX("setNx",System.currentTimeMillis()+"",10000000);

        Response response = new Response();
        response.setMessage("测试序列化操作");
        redisClient.set("bing.Pan",response, 60L,TimeUnit.SECONDS);
        redisClient.convertAndSend("chat01","rest redis publish subscription chat01");
        redisClient.convertAndSend("chat02","rest redis publish subscription chat02");

        Object o = redisClient.get("bing.Pan");
        return new Response<>(o);
    }


    @RequestMapping(value = "/testTransaction")
    public Object testTransaction(){
        return testService.testTransaction();

    }

    @RequestMapping(value = "/testDistributedReentrantLock")
    public Object testDistributedReentrantLock(){
        for (int x = 0; x < 100; x ++ ){
            DistributedReentrantLock lock = new DistributedReentrantLock(redisReentrantLockClient,"testDistributedReentrantLock");
            boolean b = lock.tryLock(5000, TimeUnit.MILLISECONDS);
            if(b){
                int  value = (int)redisReentrantLockClient.get("testDistributedReentrantLockValue");
                if(value >0){
                    --value;
                    redisReentrantLockClient.set("testDistributedReentrantLockValue",value);
                    LOGGER.info("【cloud-member-user】 获取到分布式锁,修改后的值为 [{}]",value);
                }else {
                    LOGGER.info("【cloud-member-user】 获取到分布式锁,获取到的值为[{}],结束循环",value);
                    lock.unlock();
                    break;
                }
                lock.unlock();

            }else {
                LOGGER.info("【cloud-member-user】 未获取到锁");
            }

        }

        return null;

    }


}
